var xmlProDoc;
var xslMngDoc;
var xmlUserDoc;
function initDoc(){
    xmlProDoc=loadSourceDoc("xml", "Products.xml");
    xmlUserDoc=loadSourceDoc("xml", "Users.xml");
    xslMngDoc=loadSourceDoc("xsl", "Management.xsl");
}
window.onload=function(){
    try{
        initDoc();
        var tabContainer=document.getElementById("admin-tab-container");
        var adminContainer=document.getElementById("admin-container");
        handleTab(tabContainer,adminContainer);
        tabContainer.onclick=function(e){
            var cur=e.target;
            var allTabs=e.currentTarget.querySelectorAll("a.admin-tab");
            for (i = 0; i < allTabs.length; i++) {
                var tab=allTabs[i];
                if(tab==cur){
                    tab.className = "admin-tab current-tab";
                    handleTab(e.currentTarget,adminContainer);
                }else{
                    tab.className = "admin-tab";
                }
            }
        }
        adminContainer.onclick=function(e){
            var currentObj=e.target;
            var currentTab=tabContainer.querySelector(".current-tab.current-tab");
            var flag=currentTab.getAttribute("flag");
            switch(flag){
                case "user":
                    handleUser(currentObj);
                    break;
                case "product":
                    handleProduct(currentObj);
                    break;
                case "order":
                    handleOrder(currentObj);
                    break;
                case "report":
                    break;
                default:
                    break;
            }
        }
        adminContainer.onchange=function(e){
            var currentObj=e.target;
            var currentTab=tabContainer.querySelector(".current-tab.current-tab");
            var flag=currentTab.getAttribute("flag");
            switch(flag){
                case "user":
                    handleOnChangeUser(currentObj);
                    break;
                case "product":
                    break;
                case "order":
                    handleOnChangeStatusOrder(currentObj);
                    break;
                case "report":
                    break;
                default:
                    break;
            }
        }
    }
    catch(error){
    }
}

function handleOnChangeUser(current){
    var errorcontainer=document.getElementById("error-container");
    var controlcontainer=document.getElementById("UserContainer");
    var manageUser=new ManagementUser(errorcontainer, controlcontainer);
    if(current.className=="updateRole"){
        var roleIdString=current.value;
        var uid=current.getAttribute("userid");
        manageUser.updateRole(uid,roleIdString);
    }
}
function handleOnChangeStatusOrder(current){
    var errorcontainer=document.getElementById("error-container");
    var controlcontainer=document.getElementById("OrderContainer");
    var order = new ManagementOrder(errorcontainer, controlcontainer)
    if(current.className == "linkUpdateStatusOrder") {
        var orderid = parseInt(current.getAttribute("orderid"));
        var status = current.options[current.selectedIndex].text;

        order.updateOrder(orderid, status);
    }
}
function handleUser(current){
    var errorcontainer=document.getElementById("error-container");
    var controlcontainer=document.getElementById("UserContainer");
    var manageUser=new ManagementUser(errorcontainer, controlcontainer);
    var link = current.className.split(' ')[2];
    if(link=="linkActive"){
        var uid=current.getAttribute("userid");
        manageUser.toggleActivation(uid);
    }
    if(link=="linkDetail"){
        var udetailid=current.getAttribute("userdetailid");
        manageUser.viewUserDetail(udetailid);
    }
    
}
function handleProduct(current){
    var errorcontainer=document.getElementById("error-container");
    var controlcontainer=document.getElementById("ProductContainer");
    var product = new ManagementProduct(errorcontainer, controlcontainer)
    var checkUpdatePro = current.className.split(' ');
    if (checkUpdatePro.length == 3)
    {
        if(checkUpdatePro[2]=="linkUpdate"){
            var parent=getParentByTagName(current,"tr");
            var proName=parent.getElementsByClassName("ProName")[0];
            var proImg=parent.getElementsByClassName("ProImage")[0];
            var proDescription=parent.getElementsByClassName("ProDescription")[0];
            var proPrice=parent.getElementsByClassName("ProPrice")[0];
            if(current.innerHTML=="Update"){
                current.innerHTML="Done";
                createTextInputFromDiv(proName);
                //createTextInputFromDiv(proImg);
                createTextInputFromDiv(proDescription);
                createTextInputFromDiv(proPrice);
            }else{
                var proid=current.attributes["proid"].value;
                product.updateProduct(current,proid, proName.value, proImg, proDescription.value, proPrice.value);
            }
        }
    }    
    if(current.className=="ViewComments"){
        var rid=current.getAttribute("proid");
        var rname=current.getAttribute("proname");
        product.viewComments(rid,rname);
    }
    if(current.className=="linkActive"){
        var rid=current.attributes["proid"].value;
        product.toggleActivation(rid);
    }

    var checkAddProduct = current.className.split(' ');
    if (checkAddProduct.length == 3)
    {
        if(checkAddProduct[2]=="linkAdd"){
            product.viewAdd();
        }
    }
}

function handleOrder(current){
    var errorcontainer=document.getElementById("error-container");
    var controlcontainer=document.getElementById("OrderContainer");
    var order = new ManagementOrder(errorcontainer, controlcontainer)
    var link = current.className.split(' ')[2];
    if(link=="linkDetail"){
        var orderid = parseInt(current.getAttribute("orderid"));
        var Closer = document.getElementById("btnClose");
        var Overlay = document.getElementById("fadeOverlay");
        var ContainerElement = document.getElementById("PopupContainer");
        var Content = document.getElementById("content");
        var orderString = loadOrderDetailAdmin(orderid);
        var popupOrder = new Lightbox(Closer, Overlay, ContainerElement, Content);

        Content.innerHTML = orderString;
        popupOrder.show();
    } else if(link == "linkUpdate") {
        var orderid = parseInt(current.getAttribute("orderid"));
        var select = current.parentElement.parentElement.getElementsByClassName("order-status-select")[0];
        var status = select.options[select.selectedIndex].text;

        order.updateOrder(orderid, status);
    }
    
}
function handleTab(tabContainer,adminContainer){
    var tab=tabContainer.querySelector(".current-tab.current-tab");
    if(tab&&tab.attributes["flag"]){
        var flag=tab.getAttribute("flag");
        var view;
        switch(flag){
            case "user":
                view=transformViewUser();
                break;
            case "product":
                view=transformViewProduct();
                break;
            case "order":
                transformViewOrder();
                break;
            case "report":
                view=transformViewReport();
                break;
            default:
                break;
        }
        adminContainer.innerHTML=docfragToString(view);
    }
}
function transformViewUser(){
    var param=[{
        name:"type",
        value:"containuser"
    }];

    xmlUserDoc=loadSourceDoc("xml", "Users.xml");
    var view=transformXmlAndXsl(xmlUserDoc, xslMngDoc, param);
    return view;
}
function transformViewProduct(){
    var param=[{
        name:"type",
        value:"containproduct"
    }];
    xmlProDoc=loadSourceDoc("xml", "Products.xml");
    var view=transformXmlAndXsl(xmlProDoc, xslMngDoc, param);
    return view;
}
function transformViewReport(){
    var param=[{
        name:"type",
        value:"report"
    },{
        name:"contextpath",
        value:contextPath
    },{
        name:"reportholder",
        value:"ReportController"
    }];
    var view=transformXmlAndXsl(xmlProDoc, xslMngDoc, param);
    return view;
}
function transformViewOrder(){
    var adminContainer=document.getElementById("admin-container");
    var xmlhttp;
    var data="action=TRANSFORM_ORDER_ON_ADMIN_PAGE";
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET", contextPath+"/FlowController?"+data, false);
    xmlhttp.send();

    var orderString = "<div id='error-container'></div><div id='OrderContainer' class='manage-content'>" + xmlhttp.responseText + "</div>";
    adminContainer.innerHTML = orderString;

}
function loadOrderDetailAdmin(id){
    var xmlhttp;
    var data="action=TRANSFORM_ORDERDETAIL_ADMINPAGE&orderid=" + id;
    xmlhttp=getXmlHttpObj();
    xmlhttp.open("GET", contextPath+"/FlowController?"+data, false);
    xmlhttp.send();
    return xmlhttp.responseText;
}